﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CKS.Core.Framework
{
    public static partial class Ext
    {
        public static List<T> GetRandom<T>(this List<T> source, int count)
        {
            List<T> cd = new List<T>(source);
            List<T> res = new List<T>();
            if (cd.Count < count)
            {
                res.AddRange(cd);
                return res;
            }
            for (int i = 0; i < count; i++)
            {
                Random rd = new Random();
                int k = rd.Next(cd.Count);
                res.Add(cd[k]);
                cd.RemoveAt(k);
            }
            return res;
        }


        public static bool Remove<T,K>(this ConcurrentDictionary<T,K> dictionary, T key)
        {
            try
            {
                K val = default(K);
                return  dictionary.TryRemove(key, out val);
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Add<T, K>(this ConcurrentDictionary<T, K> dictionary, T key,K val)
        {
            try
            {
                
                return dictionary.TryAdd(key, val);
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
